package cn.kgc.controller.logic.hr;
import cn.kgc.pojo.*;
import cn.kgc.service.hr.QingJiaService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wang
 * @date: 2022/2/8
 * @description:
 */
@Controller
@RequestMapping("/wxw")
public class WxwController2 {

    @Resource
    private QingJiaService qingJiaService;


//    @Resource
//    private loginUtils loginUtils;
    @RequestMapping("/QingJia")
    @ResponseBody
    public String QingJiaGL(){
        List<Dict> dict = qingJiaService.selDictQ(1);
        return JSON.toJSONString(dict);
    }

    @RequestMapping("/ChuChai")
    @ResponseBody
    public String ChuChai(){
        List<Dict> dict = qingJiaService.selDictQ(2);
        return JSON.toJSONString(dict);
    }


    @RequestMapping("/TianShu")
    @ResponseBody
    public Long TianShu(@RequestParam String shiJian1, @RequestParam String shiJian2) throws ParseException {
        System.out.println(123);
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date star = dft.parse(shiJian1);//开始时间
        Date endDay=dft.parse(shiJian2);//结束时间
        Long starTime=star.getTime();
        Long endTime=endDay.getTime();
        Long num=endTime-starTime;//时间戳相差的毫秒数
        Long i = (num/24/60/60/1000);
        System.out.println("相差天数为："+num/24/60/60/1000);//除以一天的毫秒数
        return i;
    }



    @RequestMapping("/selShen")
    @ResponseBody
    public String selShen(HttpSession session){
        User user = (User)session.getAttribute("user");
        int num = user.getJt_id();
        Department s = qingJiaService.selZhiW(num);
        int i= num-1;
        String i1 = qingJiaService.selShen(i);
        if (i1 == null||i1=="") {
            List<String> strings = qingJiaService.selName(9);
            return JSON.toJSONString(strings);
        }
        int i2= Integer.valueOf(i1);
        if (i2==s.getId()){
            List<String> strings = qingJiaService.selName(i);
            return JSON.toJSONString(strings);
        }else{
            List<String> strings = qingJiaService.selName(9);
            return JSON.toJSONString(strings);
        }
    }
    @RequestMapping("/QingJiaSave")
    @ResponseBody
    public boolean QingJiaSave(HttpSession session,@RequestParam String begintime,
                               @RequestParam String endtime,
                               @RequestParam Integer d_id,
                               @RequestParam String reason,
                               @RequestParam String approver
                               ) throws ParseException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
        Date star = fmt.parse(begintime);//开始时间
        Date endDay=fmt.parse(endtime);//结束时间
        java.sql.Date star2 = new java.sql.Date(star.getTime());
        java.sql.Date endDay2 = new java.sql.Date(endDay.getTime());
        User user = (User)session.getAttribute("user");
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");
        Leaveapply l = new Leaveapply(-1,user.getId(),jobtitle.getId(),star2,endDay2,d_id,reason,approver,0);
        boolean b = qingJiaService.addLe(l);
        return b;
    }

    @RequestMapping("/ChuCaiSave")
    @ResponseBody
    public boolean ChuCaiSave(HttpSession session,@RequestParam String begintime,
                               @RequestParam String endtime,
                               @RequestParam Integer d_id,
                               @RequestParam String site,
                               @RequestParam String reason,
                               @RequestParam String mission,
                               @RequestParam double money,
                               @RequestParam String approver
    ) throws ParseException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
        Date star = fmt.parse(begintime);//开始时间
        Date endDay=fmt.parse(endtime);//结束时间
        java.sql.Date star2 = new java.sql.Date(star.getTime());
        java.sql.Date endDay2 = new java.sql.Date(endDay.getTime());
        User user = (User)session.getAttribute("user");
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");
        Travelrequest travelrequest = new Travelrequest(-1,user.getId(),jobtitle.getId(),star2,endDay2,d_id,site,reason,mission,money,approver,0);
        boolean b = qingJiaService.addTr(travelrequest);
        return b;
    }
    @RequestMapping("/QLJL")
    @ResponseBody
    public String QLJL(HttpSession session,Integer currentPage,Integer pageSize,Integer state){
        PageHelper.startPage(currentPage,pageSize);
        User user = (User)session.getAttribute("user");
        List<Leaveapply> leaveapplies = qingJiaService.selBy(user.getId(),state);
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");
        PageInfo<Leaveapply> pageInfo = new PageInfo<>(leaveapplies);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("user",user);
        map.put("lea",pageInfo);
        map.put("jobtitle",jobtitle);
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }
    @RequestMapping("/chuchai")
    @ResponseBody
    public String chuchai(HttpSession session,Integer currentPage2,Integer pageSize2,Integer state){
        PageHelper.startPage(currentPage2,pageSize2);
        User user = (User)session.getAttribute("user");
        List<Travelrequest> travelrequests = qingJiaService.selByTr(user.getId(),state);
        Jobtitle jobtitle = (Jobtitle)session.getAttribute("zhiWei");
        PageInfo<Travelrequest> pageInfo = new PageInfo<>(travelrequests);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("user",user);
        map.put("tr",pageInfo);
        map.put("jobtitle",jobtitle);
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
    }

    @RequestMapping("/updLe")
    @ResponseBody
    public boolean updLe(Integer id){
        boolean b = qingJiaService.updLe(id);
        return b;
    }
    @RequestMapping("/updTr")
    @ResponseBody
    public boolean updTr(Integer id){
        boolean b = qingJiaService.updTr(id);
        return b;
    }
    @RequestMapping("/selBychuchai/{id}")
    public String selBychuchai(@PathVariable Integer id, Model model){
        List<Dict> dicts = qingJiaService.selDictQ(2);
        Travelrequest travelrequest = qingJiaService.selBytr(id);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        model.addAttribute("tra",travelrequest);
        model.addAttribute("dec",dicts);
        return "/hr/wxw-chuchaixiugai";
    }
    @RequestMapping("/selByqingjia/{id}")
    public String selByqingjia(@PathVariable Integer id, Model model){
        List<Dict> dicts = qingJiaService.selDictQ(1);
        Leaveapply leaveapply = qingJiaService.selByle(id);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        model.addAttribute("lea",leaveapply);
        model.addAttribute("dec",dicts);
        Long starTime=leaveapply.getBegintime().getTime();
        Long endTime=leaveapply.getEndtime().getTime();
        Long num=endTime-starTime;//时间戳相差的毫秒数
        Long i = (num/24/60/60/1000);
        model.addAttribute("tian",i);
        return "/hr/wxw-qj2";
    }


}

